package dobest.ray.com.jiongjiong.activity.HomePage.statue.emr;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

import dobest.ray.com.jiongjiong.App;
import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.activity.HomePage.HomePagePresenter;
import dobest.ray.com.jiongjiong.adapter.ArchivesListAdapter;
import dobest.ray.com.jiongjiong.bean.ArchivesListBean;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;
import dobest.ray.com.jiongjiong.constants.Constant;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by Ray on 2016/4/8.
 * https://github.com/ray0807
 * 电子病历
 */
public class ElectronicMedicalRecordActivity extends BaseActivity<HomePagePresenter> implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener, PresenterCallBack {
    private PullToRefreshListView ptr_list;
    private ArchivesListAdapter adapter;

    private boolean isRefresh = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electronic_medical_record);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        setPresenter(new HomePagePresenter(), this);
        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setTitle("档案");
        navi.setBackListener(R.mipmap.img_back, "");
        ptr_list = (PullToRefreshListView) findViewById(R.id.ptr_list);
    }

    @Override
    public void init() {
        ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new ArchivesListAdapter(this);
        ptr_list.getRefreshableView().setAdapter(adapter);
        ptr_list.setOnRefreshListener(this);

        loadingDialog.show();
        getPresenter().getArchiveList(this, App.getInstance().uid, isRefresh);
    }

    @Override
    public void addListeners() {
        ptr_list.setOnItemClickListener(this);
    }

    /**
     * 下拉刷新
     *
     * @param refreshView
     */
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        isRefresh = true;
        getPresenter().getArchiveList(this, "158", isRefresh);

    }

    /**
     * 上拉加载更多
     *
     * @param refreshView
     */
    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isRefresh = false;
        getPresenter().getArchiveList(this, "158", isRefresh);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RecordDetailsActivity.startRecordDetailActivity(ElectronicMedicalRecordActivity.this, adapter.getItem(position - 1).type + "", adapter.getItem(position - 1).id + "");
    }

    private List<ArchivesListBean> ListBean;

    @Override
    public <T> void callBackPresenter(PresenterData<T> data) {
        if (loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        T t = data.data;
        if (t == null) {
            if (ptr_list.isRefreshing())
                ptr_list.onRefreshComplete();
            return;
        }
        if (Constant.ARCHIVE_LIST_BEAN_TAG.equals(data.tag)) {
            ListBean = (List<ArchivesListBean>) t;
            if (ListBean != null && ListBean.size() > 0) {
                if (isRefresh) {
                    adapter.refresh(ListBean);
                } else {
                    adapter.setData(ListBean);
                }
            }
            ptr_list.onRefreshComplete();
        }
    }
}
